package com.zz.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zz.domain.*;
import com.zz.services.ToolsItemService;
import com.zz.services.ToolsTypeService;
import com.zz.services.impl.BannerServiceImpl;
import com.zz.services.impl.CourseTopicServiceImpl;
import com.zz.services.impl.CourseTypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Controller
public class PageController {
    @Autowired
    CourseTopicServiceImpl courseTopicService;
    @Autowired
    BannerServiceImpl bannerService ;
    @Autowired
    CourseTypeServiceImpl courseTypeService;
    @Autowired
    ToolsTypeService toolsTypeService;
    @Autowired
    ToolsItemService toolsItemService ;
    //首页
    @RequestMapping("/")
    public ModelAndView indexPage(){
        ModelAndView mv = new ModelAndView();
//        获取首页数据
        //获取首页横幅数据
        PageInfo<Banner> indexBanner = bannerService.getIndexBanner();
        //最新
        //设置分页 第几页 显示 几个
        PageInfo<CourseTopic> newestTopicList = courseTopicService.getIndexNewestTopic(4);
        //type = 3
        //设置分页,第几页,显示n个
        PageInfo<CourseTopic> courseTopicList = courseTopicService.getIndexCourseTopic(3);
        mv.addObject("courseTopicList",courseTopicList);
        mv.addObject("newestTopicList",newestTopicList);
        mv.addObject("indexBanner",indexBanner.getList());
        mv.addObject("focusIndex",0); //header选中的项
        mv.setViewName("index");
        return  mv;
    }

    // vip
    @RequestMapping(value = "/vip")
    public String vip(Model model) {
        model.addAttribute("focusIndex",2); //header选中的项
        return "vip";
    }

    //课程列表
    @RequestMapping(value = "/courseList")
    public String courseList(Model model) {
        PageHelper.startPage(1,16);
        PageInfo<CourseTopic> courseList =  courseTopicService.getCourseTopicList();
        List<CourseType> courseTypeList =  courseTypeService.getCourseTypeAll();
        model.addAttribute("courseTypeList",courseTypeList);
        model.addAttribute("courseTopicList",courseList);
        model.addAttribute("typeId",0); //默认所有
        model.addAttribute("focusIndex",1); //header选中的项
        return "course_list";
    }
    //按照课程分类 课程列表
    @RequestMapping(value = "/courseList/{typeId}")
    public String courseListType(Model model, @PathVariable Integer typeId,Integer pageNum) {
        if (pageNum == null || pageNum <1){
            pageNum =1 ;
        }
        PageHelper.startPage(pageNum,16);
        PageInfo<CourseTopic> courseList = null ;
        if (typeId == null || typeId <1){
            //获取所有课程列表
            courseList =   courseTopicService.getCourseTopicList();
        }else {
            //获取分类课程列表
            courseList = courseTopicService.getCourseTopicByTypeId(typeId);
        }
        List<CourseType> courseTypeList =  courseTypeService.getCourseTypeAll();
        model.addAttribute("courseTypeList",courseTypeList);
        model.addAttribute("courseTopicList",courseList);
        model.addAttribute("typeId",typeId);
        model.addAttribute("focusIndex",1);//header选中的项
        return "course_list";
    }

    //工具
    @RequestMapping(value = "/tools")
    public String toolsPage(Model model) {
        model.addAttribute("focusIndex",4); //header选中的项
        List <ToolsType> typeLit = toolsTypeService.getToolsTypeAll();
        PageHelper.startPage(1,16);
        PageInfo<ToolsItem> toolsItems =  toolsItemService.getToolsItemALl();
        model.addAttribute("toolsTypeList",typeLit);//工具的分类
        model.addAttribute("toolsItems",toolsItems);//工具页显示的内容
        model.addAttribute("typeId",0);
        return "tools";
    }

    //按分类查找工具
    @RequestMapping(value = "/tools/{typeId}")
    public String toolsType(Model model,@PathVariable Integer typeId,Integer pageNum) {
        if (pageNum == null || pageNum <1){
            pageNum =1 ;
        }
        List <ToolsType> typeLit = toolsTypeService.getToolsTypeAll();
        PageHelper.startPage(pageNum,16);
        PageInfo<ToolsItem> toolsItems = null;
        if (typeId == 0 ){
            toolsItems =  toolsItemService.getToolsItemALl();
        }else {
            toolsItems = toolsItemService.getToolsItemByTypeId(typeId);
        }
        model.addAttribute("focusIndex",4); //header选中的项
        model.addAttribute("toolsTypeList",typeLit);//工具的分类
        model.addAttribute("toolsItems",toolsItems);//工具页显示的内容
        model.addAttribute("typeId",typeId); //工具类型id
        return "tools";
    }


}
